package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ToxicBakery.viewpager.transforms.RotateUpTransformer;
import com.blankj.utilcode.util.SPUtils;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.IntroductionPageAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.base.activity.BaseActivity;
import com.huifaapp.mall.base.basemvp.IPresenter;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nineoldandroids.view.ViewHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class IntroductionPageActivity extends BaseActivity {
    @BindView(R.id.vp_introductionpage)
    ViewPager mIntroductionPage;
    @BindView(R.id.tv_hint)
    TextView tv_hint;

    private IntroductionPageAdapter adapter;
    private List<ImageView> imagelist;
    private boolean canJump;
    private int curPosition;
    private int lastIndex;
    private int[] images = new int[]{
            R.drawable.w1,
            R.drawable.w2,
            R.drawable.w3,
//            R.drawable.welcom4,
    };

    @Override
    protected boolean translucentStatusBar() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_introductionpage;
    }

    @Override
    protected void initData(Bundle bundle) {
        imagelist = new ArrayList<ImageView>();
        int[] imgs = new int[]{R.drawable.w1, R.drawable.w2, R.drawable.w3};
        for (int i = 0; i < images.length; i++) {
            ImageView img = new ImageView(this);
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            img.setImageResource(imgs[i]);
            img.setBackgroundResource(imgs[i]);

            imagelist.add(img);

        }
        lastIndex = images.length - 1;
        adapter = new IntroductionPageAdapter(IntroductionPageActivity.this, imagelist);
        mIntroductionPage.setAdapter(adapter);
        mIntroductionPage.setPageTransformer(true, new RotateUpTransformer());
        mIntroductionPage.setOnPageChangeListener(changeListener);
        tv_hint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtils.getInstance().put(SPConstants.KEY_YINDAO, true);
                startActivity(new Intent(IntroductionPageActivity.this, LoginActivity.class));
                finish();
            }
        });
//
    }

    @Override
    protected IPresenter onLoadPresenter() {
        return null;
    }

    @SuppressWarnings("deprecation")
//    @Override
//    protected void initDate() {// 注：没有写最后一张的点击事件
//
//    }

    private OnPageChangeListener changeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            curPosition = position;
            AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
            animation.setDuration(1500);
            tv_hint.startAnimation(animation);
            if (curPosition == lastIndex) {
                tv_hint.setVisibility(View.VISIBLE);
                tv_hint.setText("立即体验");
                tv_hint.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SPUtils.getInstance().put(SPConstants.KEY_YINDAO, true);
                        startActivity(new Intent(IntroductionPageActivity.this, LoginActivity.class));
                        finish();
                    }
                });

                animation = new AlphaAnimation(0.3f, 1.0f);
                animation.setDuration(1500);
                tv_hint.startAnimation(animation);
            } else {
                tv_hint.setVisibility(View.GONE);
                tv_hint.setText("滑动进入");
                tv_hint.setOnClickListener(null);
                imagelist.get(position).setOnClickListener(null);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (position == lastIndex && positionOffset == 0 && positionOffsetPixels == 0) {
                if (canJump) {
                    //滑动进入代码
                }
                // 事件执行一次后进行重置,避免事件多次触发
//                user.saveWelcome("ok");
//                startActivity(new Intent(IntroductionPageActivity.this, MainActivity.class));
            //    finish();
                LoadIsShow();

                canJump = false;
            }
        }

        private void LoadIsShow() {
            String url = ApiManager.INDEX_ISSHOWCREMONEY;
            Log.e("主题","主题url===="+url);
            HttpUtils http = new HttpUtils();
            http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    try {
                        JSONObject obj=new JSONObject(responseInfo.result);
                        if(obj.getString("code").equals("10000")) {
                            if(obj.getString("data").equals("hide")){
                                Intent intent = new Intent(IntroductionPageActivity.this,MainFragment.class);
                                startActivity(intent);
                            }else{
                                Intent intent = new Intent(IntroductionPageActivity.this,WelcomeActivityone.class);
                                startActivity(intent);
                            }
                        }else {
                            Toast.makeText(IntroductionPageActivity.this, obj.getString("msg"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override

                public void onFailure(HttpException e, String s) {
                    Toast.makeText(IntroductionPageActivity.this,"失败",Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_DRAGGING && curPosition == lastIndex) {
                canJump = true;
            } else {
                canJump = false;
            }
        }
    };

    class RotateDownPageTransformer implements ViewPager.PageTransformer {

        private static final float ROT_MAX = 20.0f;
        private float mRot;

        public void transformPage(View view, float position) {

            if (position < -1) {
                ViewHelper.setRotation(view, 0);

            } else if (position <= 1) {
                if (position < 0) {
                    mRot = (ROT_MAX * position);
                    ViewHelper.setPivotX(view, view.getMeasuredWidth() * 0.5f);
                    ViewHelper.setPivotY(view, view.getMeasuredHeight());
                    ViewHelper.setRotation(view, mRot);
                } else {
                    mRot = (ROT_MAX * position);
                    ViewHelper.setPivotX(view, view.getMeasuredWidth() * 0.5f);
                    ViewHelper.setPivotY(view, view.getMeasuredHeight());
                    ViewHelper.setRotation(view, mRot);
                }

            } else {
                ViewHelper.setRotation(view, 0);
            }
        }
    }

}
